package logic

import (
	"context"
	"errors"
	"srv/models/mysql"
	"srv/models/redisInit"

	"srv/internal/svc"
	"srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddCartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCartLogic {
	return &AddCartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddCartLogic) AddCart(in *shop.AddCartRequest) (*shop.AddCartResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{
		GoodsName:  in.GoodsName,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsNum:   int(in.GoodsNum),
		GoodsState: in.GoodsState,
	}
	err := g.Create()
	if err != nil {
		logx.Error("添加失败")
		return nil, errors.New("添加失败")
	}
	redisInit.RedisCartAdd(g.ID, g.GoodsNum)
	return &shop.AddCartResponse{
		GoodsId: int64(g.ID),
	}, nil
}
